<?php

namespace App\Http\Controllers;

abstract class Controller
{
    // 把所有的 css、js 文件都抽出来 还有script 也抽出来
    public function extractJSCSS($html)
    {
        libxml_use_internal_errors(true);
        $dom = new \DOMDocument('1.0', 'UTF-8');
        $dom->encoding = 'UTF-8';
        $dom->loadHTML('<?xml encoding="UTF-8" ?>' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        libxml_clear_errors();

        $scripts = $dom->getElementsByTagName('script');
        $styles = $dom->getElementsByTagName('link');
        $jses = [];
        $csses = [];
        foreach ($scripts as $script) {
            $jses[] = $script;
        }
        foreach ($styles as $style) {
            $csses[] = $style;
        }
        foreach ($jses as $script) {
            $script->parentNode->removeChild($script);
        }
        foreach ($csses as $style) {
            $style->parentNode->removeChild($style);
        }
        // 判断css文件是否有重复
        $css_str = '';
        $css_hrefs = [];
        foreach ($csses as $style) {
            $css = $style->getAttribute('href');
            if (!in_array($css, $css_hrefs)) {
                $css_hrefs[] = $css;
                $css_str .= '<link rel="stylesheet" href="' . $style->getAttribute('href') . '">' . "\n";
            }
        }
        // 判断js文件是否有重复
        $js_str = '';
        $js_srcs = [];
        foreach ($jses as $script) {
            $js = $script->getAttribute('src');
            if (!empty($js)) {
                if (!in_array($js, $js_srcs)) {
                    $js_srcs[] = $js;
                    $js_str .= '<script src="' . $script->getAttribute('src') . '"></script>' . "\n";
                }
            } else {
                $js_str .= '<script>' . $script->textContent . '</script>' . "\n";
            }
        }
        
        $html = $dom->saveHTML();
        // 将引用实体 转成 普通字符
        $html = html_entity_decode($html);
        // 去除 <?xml encoding="UTF-8" \\\?\>
        $html = str_replace('<?xml encoding="UTF-8" ?>', '', $html);
        return  $css_str . $html . $js_str;
    }

    // 提取 js 代码
    public function extractJS($html) {
        libxml_use_internal_errors(true);
        $dom = new \DOMDocument('1.0', 'UTF-8');
        $dom->encoding = 'UTF-8';
        $dom->loadHTML('<?xml encoding="UTF-8" ?>' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        libxml_clear_errors();

        $scripts = $dom->getElementsByTagName('script');
        $jses = [];
        foreach ($scripts as $script) {
            $jses[] = $script;
        }
        foreach ($jses as $script) {
            $script->parentNode->removeChild($script);
        }

        $js_strs = [];
        $js_srcs = [];
        foreach ($jses as $script) {
            $js = $script->getAttribute('src');
            if (!empty($js)) {
                if (!in_array($js, $js_srcs)) {
                    $js_srcs[] = $js;
                }
            } else {
                $js_strs[] = $script->textContent;
            }
        }

        
        $html = $dom->saveHTML();
        // 将引用实体 转成 普通字符
        $html = html_entity_decode($html);
        // 去除 <?xml encoding="UTF-8" \\\?\>
        $html = str_replace('<?xml encoding="UTF-8" ?>', '', $html);
        return [
            'html' => $html,
            'js_srcs' => $js_srcs,
            'js_strs' => $js_strs,
        ];
    }
}
